const util = require('../../../utils/util.js');
const time = util.getTimeDian(Date.now() / 1000);
Page({
  data: {
    lock: true,
    genderArr: ["男", "女", "未知"],
    education: ["小学", "初中", "高中", "专科", "本科", "硕士", "博士"],
    hobbyArr: ["游戏", "阅读", "音乐", "运动", "动漫", "旅行", "居家", "曲艺", "宠物", "美食", "娱乐", "电影", "电视剧", "健康养生", "数码", "其他"],
    industryArr: ["IT/互联网/通信/电子", "金融/投资/财会", "市场/销售/客服", "广告/出版/媒体/艺术", "人事/行政/管理", "消费品/贸易/物流", "生产/制造/运营/采购", "生物/制药/医疗/护理", "咨询/法律/认证", "教育/培训/翻译", "科研/环保/休闲/其他"],
    incomeArr: ["5w以下", "5w-15w", "15w-30w", "30w以上"],
    date: null,
    addressShow: false,
    applicationTime: `${time.y}.${time.m}.${time.d}`,
    endTime: `${time.y}-${time.m}-${time.d}`,
    righticon: 'https://o6wndwjxn.qnssl.com/jsynewshopChevron.svg',
    imgUrls: getApp().globalData.imgUrls,
    current: 0,
    addressListHeader: [],
    provinceList: [],
    kindArr: [],
    second:60,
  },
  countdown: function () { //倒计时
    var that = this
    if (this.data.phone.length != 11) {
      wx.showToast({
        title: '请填写正确手机号',
        icon: 'none',
      })
      return false
    }
    that.setData({
      clicked: true,
    
    })
    util.API('spwx/v1.Msgal/personal_msg_dx', {
      phone: this.data.phone
    }).then(res => {
      let {
        data,
        code,
        msg
      } = res
      if (code == 200) {
        var time = setInterval(function () {
          that.setData({
            second: that.data.second - 1
          })
          if (that.data.second == 0) {
            clearInterval(time)
          }
        }, 1000)
      } else {
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  reGet() {
    this.setData({
      second: 60,
      ma: ""
    })
    this.countdown()
  },
  enit(){
    console.log()
    // this.data.setting.list[0].is_required == 1 
    this.setData({
      mashow: !this.data.mashow,
      phone: !this.data.mashow ? '' : this.data.setting.list[0].val,
    })
  
  },
  bindChange: function(e) {
    var data = e.currentTarget.dataset.data
    this.setData({
      [data]: e.detail.value
    })
  },
  addressShow() {
    this.setData({
      addressShow: !this.data.addressShow
    })
  },
  bindinput(e) {
    this.setData({
      [e.currentTarget.dataset.kind]: e.detail.value
    })

  },
  select(e) {
    var that = this
    var {
      provinceList,
      addressListHeader
    } = this.data;

    if (that.data.lock) {} else {
      return
    }
    if (e != 'province') {
      var dataset = e.currentTarget.dataset;
      if (dataset.indexs == 0) {
        provinceList = provinceList.slice(0, 1)
      }
    } else {
      provinceList = []
      var dataset = {
        indexs: 5
      }
    }
    let config = {}
    var url = ''
    switch (dataset.indexs) {
      case 0:
        config.oneid = dataset.id
        break;
      case 1:
        config.twoid = dataset.id
        break;
      case 2:
        config.threeid = dataset.id
        break;
      case 3:
        config.fourid = dataset.id
        break;
      case 5:
        break;
    }

    util.API('spwx/v1.Address/fourLevelLinkage', config).then(res => {
      var data = {
        name: dataset.name,
        indexs: dataset.indexs,
        id: dataset.id,
      }
      that.setData({
        lock: false
      })
      if (res.code == 200) {
        if (dataset.indexs != 5) {
          provinceList.splice(dataset.indexs + 1, provinceList.length - (dataset.indexs + 1), res.data)
          addressListHeader.splice(dataset.indexs, addressListHeader.length, data)
          addressListHeader.push({
            name: "请选择"
          })
        } else {
          provinceList.splice(0, 1, res.data)
          addressListHeader.splice(0, 1, {
            name: "请选择"
          })
        }
        that.setData({
          provinceList,
          addressListHeader,
        })
        that.setData({
          current: dataset.indexs != 5 ? dataset.indexs + 1 : 0
        })
      } else {
        addressListHeader.splice(provinceList.length - 1, 1, data)
        that.setData({
          current: provinceList.length - 1,
          addressShow: false,
          addressListHeader
        })
      }
      setTimeout(function() {
        that.setData({
          lock: true
        })
      }, 500)
    })

  },
  bindchangeS(e) {
    if (this.data.lock) {
      this.setData({
        current: e.detail.current
      })
    }
  },
  selectG(e) {
    if (this.data.lock && e.currentTarget.dataset.id) {
      this.setData({
        current: e.currentTarget.dataset.indexs
      })
    }
  },
  onLoad: function(options) {
    this.getInformation()
  },
  forEach() {
    var that = this
    return new Promise(function(resolve, reject) {
      that.data.kindArr = [
        that.data.phone,
        that.data.name,
        that.data.birthday,
        that.data.identity_card,
        that.data.addressListHeader,
        that.data.email,
        that.data.gender,
        that.data.edu_background,
        that.data.hobby,
        that.data.income,
        that.data.industry,
      ]
      resolve(true)
    })

  },
  getInformation() {
    var that = this,
      {
        provinceList
      } = this.data
    util.API('spwx/v1.member/setCustomerData', {}).then(res => {
      let {
        code,
        data
      } = res
      data.list.forEach((item, index) => {
        var dynamic = item.config_var
      
        if (item.config_var == "address") {
          item.val = item.val? JSON.parse(item.val):false
        }
        this.setData({
          [dynamic]: item.val
        })
      })
      var adddrssIndex = data.list[4].val
      if (!adddrssIndex) {
        this.select('province')
      } else {
        this.setData({
          addressListHeader: adddrssIndex,
        })
        var then = this.getAddressList()
        var then0 = this.getAddressList({
          oneid: adddrssIndex[0].id
        })
        var then1 = this.getAddressList({
          twoid: adddrssIndex[1].id
        })
        var then2 = adddrssIndex[2] ? this.getAddressList({
          threeid: adddrssIndex[2].id
        }) : ""
        // var then3 = adddrssIndex[3] ? this.getAddressList({
        //   fourid: adddrssIndex[3].id
        // }) : "";
        Promise.all([then, then0, then1, then2, ]).then(function(value) {
          that.setData({
            provinceList: value,
          })
        });
      }
      this.setData({
        setting: data,
        mashow: !data.list[0].val
      })
    
    })

  },
  getAddressList(config) {
    var {
      provinceList
    } = this.data, that = this
    return new Promise(function(resolve, reject) {
      util.API('spwx/v1.Address/fourLevelLinkage', config).then(res => {
        resolve(res.data)
      })
    })
  },
  onShow: function() {},
  formSubmit(e) {
    if (e.detail.userInfo) {
      var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      if (this.data.email) {
        if (!pattern.test(this.data.email)) {
          wx.showToast({
            title: '邮箱格式不正确',
            icon: "none"
          })
          return
        }
      }
      if (!this.data.addressListHeader[this.data.addressListHeader.length - 1].id) {
        wx.showToast({
          title: '请选择准确地址',
          icon: "none"
        })
        return
      }
      if (this.data.setting.list[0].is_required==1 ) {
      if(!this.data.phone){
        wx.showToast({
          title: '请填写手机号',
          icon: "none"
        })
        return
      }
      }
      if (this.data.mashow ) {
        if (!this.data.verify){
          wx.showToast({
            title: '请填写验证码',
            icon: "none"
          })
          return
        }
      }
     
      // this.triggerEvent('myevent', {
      //   'showSpecification': !this.data.showSpecification
      // })
      this.forEach().then(res => {
        let lindArr = []
        var setting_li = this.data.setting.list
        setting_li.forEach((item, index) => {
          if (item.config_var == 'address') {
            var val = this.data.kindArr[index] ? JSON.stringify(this.data.kindArr[index]) : ""
          } else {
            var val = this.data.kindArr[index] ? this.data.kindArr[index] : ""
          }
          lindArr.push({
            config_var: item.config_var,
            config_val: item.config_val,
            is_required: item.is_required,
            is_update: item.is_update,
            setting_id: item.setting_id,
            val
          })
        })
        util.API('spwx/v1.member/setCustomerData', {
          json: JSON.stringify(lindArr),
          flag: 'save',
          verify: this.data.verify ? this.data.verify : "" 
        }).then(res => {
          let {
            code,
            data
          } = res
          if (code == 200) {
            wx.showToast({
              title: '提交成功',
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
            })
          }
          this.getInformation()
          this.setData({
            verify:""
     
          })
        })
      })
      util.API('wechat/authorize/updateapp', {
        nick_name: e.detail.userInfo.nickName,
        cover: e.detail.userInfo.avatarUrl
      })
    } else {
      wx.showModal({
        title: '授权失败',
        content: '提交信息需要授权',
        showCancel: true,
        success: function(res) {
          if (!res.confirm) {
            wx.navigateBack({
              delta: 1,
            })
          }
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }




  },
})